/***************************
新增or编辑
* */
<template>
  <el-dialog
    :title="dialogTitle"
    :close-on-click-modal="false"
    :visible.sync="visible"
    top="8vh"
    @close="closeDialog()"
    width="70vw"
  >
    <div >
      <div class="boxsty">
    <el-form
      ref="dataForm"
      :rules="formRules"
      :model="dataForm"

      label-width="130px"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-row>
        <el-col :span="24">
          <el-col :span="12">
      <el-form-item label="日常考核名称:" prop="name">
        <el-input v-model="dataForm.name"></el-input>
      </el-form-item>
          </el-col>
        </el-col>
      </el-row>
      <el-row>
      <el-col :span="24">
        <el-col :span="12">
      <el-form-item label="周期名称:" prop="period" >
        <el-select style="width:100%" v-model="dataForm.period" placeholder="请选择">
          <el-option
            v-for="item in dateOptions"
            :key="item.label"
            :label="item.label"
            :value="item.label"
          ></el-option>
        </el-select>
      </el-form-item>
        </el-col>
        <el-col :span="1">
          &nbsp;
        </el-col>
        <el-col :span="11">
          <el-button plain type="primary" @click="planPush">生成指标考核数据</el-button>
        </el-col>
      </el-col>
      </el-row>
      <el-row>
      <el-col :span="24">
        <el-col :span="12">
      <el-form-item label="开始时间:" prop="assessBeginTime">
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="dataForm.assessBeginTime"
          style="width:100%"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
        </el-col>
          <el-col :span="12">
      <el-form-item label="结束时间:" prop="assessEndTime" >
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="dataForm.assessEndTime"
          style="width:100%"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
        </el-col>
      </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-col :span="12">
      <el-form-item label="填报开始时间:" prop="completeBeginTime" >
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="dataForm.completeBeginTime"
          style="width:100%"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
          </el-col>
          <el-col :span="12">
      <el-form-item label="填报结束时间:" prop="completeEndTime">
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="dataForm.completeEndTime"
          style="width:100%"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
          </el-col>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-col :span="6">
          <el-form-item label="开始时间+" prop="autoFillDays" >
            <el-select style="width:100%" v-model="dataForm.autoFillDays" placeholder="请选择">
              <el-option
                v-for="item in days"
                :key="index"
                :label="item.name"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          </el-col>
          <el-col :span="18" style="height: 52px;line-height: 35px;">
            &nbsp;&nbsp;<span style="padding-top: 10px;font-size: 16px;color: #303745;">天,指标考核数据自动推送到履约中。</span>
          </el-col>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-col :span="24" >
            <span style="margin-left: 20px;color:red;font-size: 16px;">注明:如开始时间是2022-01-01，加5天后，2022-01-06早上03:00执行定时任务。</span>
          </el-col>
        </el-col>
      </el-row>
    </el-form>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" plain @click="handleConfirm">确定</el-button>
      <el-button type="primary" plain @click="closeDialog()">返回</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  addExamine,
  updateExamine
} from "@/api/dailyExamine/targetExamine/examineStart"; //接口api
import { createTarget,checkCurTarget } from "@/api/dailyExamine/targetExamine/sjtemplate";
export default {
  data() {
    return {
      dialogTitle: "新增",
      visible: false,
      dataForm: {
        name: ""
      },
      dateOptions: [],
      formRules: {
        name: [{ required: true, message: "请输入考核名称", trigger: "blur" }],
        period: [
          { required: true, message: "请选择考核周期", trigger: "change" }
        ],
        assessBeginTime: [
          { required: true, message: "请选择开始时间", trigger: "change" }
        ],
        assessEndTime: [
          { required: true, message: "请选择结束时间", trigger: "change" }
        ],
        completeBeginTime: [
          { required: true, message: "请选择填报开始时间", trigger: "change" }
        ],
        completeEndTime: [
          { required: true, message: "请选择填报结束时间", trigger: "change" }
        ]
      },
        days:[
            { name: "1", value: "1" },
            { name: "2", value: "2" },
            { name: "3", value: "3" },
            { name: "4", value: "4" },
            { name: "5", value: "5" },
            { name: "6", value: "6" },
            { name: "7", value: "7" },
            { name: "8", value: "8" },
            { name: "9", value: "9" },
            { name: "10", value: "10" },
            { name: "11", value: "11" },
            { name: "12", value: "12" },
            { name: "13", value: "13" },
            { name: "14", value: "14" },
            { name: "15", value: "15" },

        ],

    };
  },
  created() {
    this.initDate();
  },
  methods: {
    //生成指标考核数据
    planPush(){

        checkCurTarget(
            Object.assign(
              {
                  period:this.dataForm.period
              }
            )
        )
            .then(res => {
            if(res.data.code == 0){
                if(res.data.data){
                    createTarget(
                      Object.assign(
                        {
                            period:this.dataForm.period
                        }
                      )
                    )
                        .then(r => {
                            if(r.data.code == 0){
                                this.$message.success("生成成功");

                            } else {
                                this.$message.error(r.data.msg);

                            }

                        })
                        .catch(err => {

                        });

                }else {
                    this.$message.error("考核周期内有重复数据，不执行");

                }

            }

        })
            .catch(err => {

            });

    },
    // 确定
    handleConfirm() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          if (this.dataForm.id) {
            updateExamine(Object.assign(this.dataForm))
              .then(r => {
                if (r.data.code == 0) {
                  this.$message.success("编辑成功");
                  this.$emit("refreshDataList");
                  this.closeDialog();
                } else {
                  this.$message.error(r.data.msg);
                }
              })
              .catch(err => {});
          } else {
              this.dataForm['assessState'] = 0;
              this.dataForm['completeState'] = 0;
            addExamine(Object.assign(this.dataForm))
              .then(r => {
                if (r.data.code == 0) {
                  this.$message.success("新增成功");
                  this.$emit("refreshDataList");
                  this.closeDialog();
                } else {
                  this.$message.error(r.data.msg);
                }
              })
              .catch(err => {});
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //初始化日期选项
    initDate() {
      let year = new Date().getFullYear();
      for (let index = 0; index < 12; index++) {
        this.dateOptions.push({
          label: `${year}年${index + 1}月`,
          value: index + 1
        });
      }
    },
    // 初始化
    init(data) {
      if (data.id) {
        this.dataForm = data;
        this.dialogTitle = "编辑";
      } else {
          this.dataForm.autoFillDays = 5;
      }
      this.visible = true;
    },
    // 表单提交
    dataFormSubmit() {},
    // 关闭
    closeDialog() {
      this.visible = false;
      this.$emit("closeDialog");
    }
  }
};
</script>

<style>
</style>
